from sklearn.feature_extraction import DictVectorizer

data = [{'city': '北京', 'temperature': 100},
        {'city': '上海', 'temperature': 60},
        {'city': '深圳', 'temperature': 30}]

dv = DictVectorizer(sparse=False)  # sparse 稀疏的意思
result = dv.fit_transform(data)

print(dv.get_feature_names())
# ['city=上海', 'city=北京', 'city=深圳', 'temperature']

print(result)  # 稀疏矩阵
